package oaSystem.biz.impl;


import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import oaSystem.biz.MessageBiz;
import oaSystem.entity.Employee;
import oaSystem.entity.Message;
import oaSystem.entity.MessageAttachment;
import oaSystem.entity.MessageReception;
import oaSystem.mapper.MessageAttachmentMapper;
import oaSystem.mapper.MessageMapper;
import oaSystem.mapper.MessageReceptionMapper;
@Service
@Transactional(propagation=Propagation.REQUIRED, timeout=60)
public class MessageBizImpl implements MessageBiz{
	@Autowired
	private MessageMapper messageMapper;
	@Autowired
	private MessageAttachmentMapper messageAttachmentMapper;
	@Autowired
	private MessageReceptionMapper messageReceptionMapper;
	
	@Override//获取单个邮件
	public Message fetchMessageById(Integer id) {
		return messageMapper.fetchMessageById(id);
	}

	@Override//模糊查询邮件
	public List<Message> getMessageBySearch(Integer sessionId,Integer categoryId,int pageNum, int pageSize, String search) {
		return messageMapper.getMessageBySearch(sessionId,categoryId,(pageNum-1)*pageSize, pageSize, search);
	}
	
	@Override//添加到草稿箱
	public void addMessageToDrafts(Message message, List<Employee> receiver_ids, MessageAttachment messageAttachment) {
		message.setStatus(0);
		messageMapper.addMessage(message);
		for (Employee receiver_id : receiver_ids) {
				MessageReception mr =new MessageReception(null,message,receiver_id,false,0);
				messageReceptionMapper.addMessageReception(mr);
			}
		if(messageAttachment!=null){
			messageAttachmentMapper.addMessageAttachment(messageAttachment);
		}
	}
	
	@Override//发送邮件给审批人
	public void sendMessageToApprover(Message message, Integer employeeId) {
		message.setIsSent(false);
		messageMapper.addMessage(message);
		MessageReception mr =new MessageReception(null,message,new Employee(employeeId),false,0);
		messageReceptionMapper.addMessageReception(mr);
		sendMessage(message);
	}

	@Override//发送
	public void sendMessage(Message message) {
		Message m = messageMapper.fetchMessageById(message.getId());
		m.setIsSent(true);
		m.setSendTime(new Date());
		messageMapper.updateMessage(m);
	}

	@Override//更新草稿箱
	public void updateToDrafts(Message message, List<Integer> employeeIds, MessageAttachment messageAttachment) {
		messageReceptionMapper.deleteMessageReceptionByMessageId(message.getId());
		for (Integer employeeId : employeeIds) {
				MessageReception mr =new MessageReception(null,message,new Employee(employeeId),false,0);
				messageReceptionMapper.addMessageReception(mr);
			}
		messageAttachmentMapper.addMessageAttachment(messageAttachment);
		messageMapper.updateMessage(message);
	}

	@Override//获取邮件总条数
	public int getMessageCount(Integer sessionId,Integer categoryId,String search) {
		return messageMapper.getMessageCount(sessionId,categoryId,search);
	}

	@Override//添加邮件
	public void addMessage(Message message) {
		messageMapper.addMessage(message);
	}

	//删除邮件
	@Override
	public void deleteMessageByMessageId(Integer messageId) {
		messageMapper.deleteMessageByMessageId(messageId);
	}

	@Override
	public void updateMessage(Message message) {
		messageMapper.updateMessage(message);
	}

}
